import Vue from 'vue'
let encode = require('./encoding.js');
import printUtil from '@/common/print/util.js'

function createPrintCode(type, item) {
	let bleInfo = Vue.prototype.$store.state.Bluetooth.bleInfo
	let nameHead = bleInfo.name.match(/.*?(?=-|_)/)[0]
	// let nameHead = 'GP_M322'.match(/.*?(?=-|_)/)[0]
	// let nameHead = 'CS3-2202'.match(/.*?(?=-|_)/)[0]
	let type_head = type + '-' + nameHead
	let qrcodeNum = parseInt(item.endNum) - parseInt(item.startNum)
	let qrcodeText = ''
	let pritCode = ''

	for (let i = 0; i <= qrcodeNum; i++) {
		qrcodeText = Vue.prototype.$qrcodeText
		qrcodeText += Vue.prototype.$SM4.encryptData_ECB(item.longtime.toString() + Vue.prototype.$prefixZero(i, 4))
		switch (type_head) {
			case 'default-CS3':
				pritCode += default_zhike(item, qrcodeText, i === 0)
				break;
			case 'simple-CS3':
				pritCode += simple_zhike(item, qrcodeText, i === 0)
				break;
			case 'default-GP':
				pritCode += default_jiabo(item, qrcodeText, i === 0)
				break;
			case 'simple-GP':
				pritCode += simple_jiabo(item, qrcodeText, i === 0)
				break;
			default:
				return false
				break;
		}
	}
	console.log(pritCode)
	let buffer
	switch (nameHead) {
		case 'CS3':
			buffer = printUtil.hexStringToBuff(pritCode)
			break;
		case 'GP':
			let uint8Array = new encode.TextEncoder(
				'gb18030', {
					NONSTANDARD_allowLegacyEncoding: true
				}).encode(pritCode)
			let uint8Buf = Array.from(uint8Array);
			buffer = new ArrayBuffer(uint8Buf.length)
			let dataView = new DataView(buffer)
			for (let j = 0; j < uint8Buf.length; j++) {
				dataView.setUint8(j, uint8Buf[j]);
			}
			break;
	}
	return buffer
}

// 芝柯标准版
function default_zhike(item, qrcodeText, isFrist) {
	let marginX = 30,
		marginY = 30,
		pageWidth = 600,
		pageHeight = 400,
		font_title = 24,
		font_content = 20,
		font_commitment = 55,
		title_margin_bottom = 12,
		content_margin_bottom = 12,
		curHeight = marginY
	let pritCode = ''

	if (isFrist && Vue.prototype.$store.getters.printCalibration) {
		pritCode += `! 0 200 200 ${pageHeight} 1\n`
		pritCode += `PAGE-WIDTH ${pageWidth}\n`
		pritCode += `GAP-SENSE\n`
		pritCode += `CENTER\n`
		pritCode += `TEXT ${font_title} 0 0 ${curHeight} —— 接下来将为您打印合格证 ——\n`
		pritCode += `FORM\n`
		pritCode += `PRINT\n`
	}
	pritCode += `! 0 200 200 ${pageHeight} 1\n`
	pritCode += `PAGE-WIDTH ${pageWidth}\n`
	pritCode += `GAP-SENSE\n`
	pritCode += `SETBOLD 2\n`
	pritCode += `CENTER\n`
	pritCode += `TEXT ${font_title} 0 0 ${curHeight} 食用农产品合格证\n`
	pritCode += `LEFT\n`
	curHeight += font_title + title_margin_bottom

	pritCode += `TEXT ${font_content} 0 ${marginX} ${curHeight} 产品名称:\n`
	pritCode += `SETBOLD 0\n`
	pritCode += `TEXT ${font_content} 0 ${marginX+100} ${curHeight} ${item.name}\n`
	curHeight += font_content + content_margin_bottom

	pritCode += `SETBOLD 2\n`
	pritCode += `TEXT ${font_content} 0 ${marginX} ${curHeight} 产品重量:\n`
	pritCode += `SETBOLD 0\n`
	pritCode += `TEXT ${font_content} 0 ${marginX+100} ${curHeight} ${item.weight}${item.unit}\n`
	curHeight += font_content + content_margin_bottom

	let wordLength, actWordLength, cnt

	pritCode += `SETBOLD 2\n`
	pritCode += `TEXT ${font_content} 0 ${marginX} ${curHeight} 产品产地:\n`
	pritCode += `SETBOLD 0\n`
	wordLength = 22
	actWordLength = item.area.length
	if (actWordLength > wordLength) {
		cnt = Math.ceil(actWordLength / wordLength)
		for (let i = 0; i < cnt; i++) {
			pritCode +=
				`TEXT ${font_content} 0 ${marginX+100} ${curHeight} ${item.area.substr(i*wordLength,wordLength)}\n`
			curHeight += font_content + content_margin_bottom
		}
	} else {
		pritCode += `TEXT ${font_content} 0 ${marginX+100} ${curHeight} ${item.area}\n`
		curHeight += font_content + content_margin_bottom
	}

	let wordLength2 = 18
	pritCode += `BARCODE QR ${marginX+wordLength2*(font_content)+30} ${curHeight} M 2 U 3\n`
	// pritCode += `MA,${item.longtime.replace(/"/g,'')+Vue.prototype.$prefixZero(item.startNum,4)}\n`
	pritCode += `MA,${qrcodeText}\n`
	pritCode += `ENDQR\n`

	pritCode += `SETBOLD 2\n`
	pritCode += `TEXT ${font_content} 0 ${marginX} ${curHeight} 产品收购者:\n`
	pritCode += `SETBOLD 0\n`
	wordLength = 12
	actWordLength = (item.buyerName || '').length
	if (actWordLength > wordLength) {
		cnt = Math.ceil(actWordLength / wordLength)
		for (let i = 0; i < cnt; i++) {
			pritCode +=
				`TEXT ${font_content} 0 ${marginX+120} ${curHeight} ${item.buyerName.substr(i*wordLength,wordLength)}\n`
			curHeight += font_content + content_margin_bottom
		}
	} else {
		pritCode += `TEXT ${font_content} 0 ${marginX+120} ${curHeight} ${item.buyerName}\n`
		curHeight += font_content + content_margin_bottom
	}

	pritCode += `SETBOLD 2\n`
	pritCode += `TEXT ${font_content} 0 ${marginX} ${curHeight} 地址:\n`
	pritCode += `SETBOLD 0\n`
	wordLength = 12
	actWordLength = (item.buyerAddress || '').length
	if (actWordLength > wordLength) {
		cnt = Math.ceil(actWordLength / wordLength)
		for (let i = 0; i < cnt; i++) {
			pritCode +=
				`TEXT ${font_content} 0 ${marginX+60} ${curHeight} ${item.buyerAddress.substr(i*wordLength,wordLength)}\n`
			curHeight += font_content + content_margin_bottom
		}
	} else {
		pritCode += `TEXT ${font_content} 0 ${marginX+60} ${curHeight} ${item.buyerAddress}\n`
		curHeight += font_content + content_margin_bottom
	}

	pritCode += `SETBOLD 2\n`
	pritCode += `TEXT ${font_content} 0 ${marginX} ${curHeight} 开具日期:\n`
	pritCode += `SETBOLD 0\n`
	pritCode += `TEXT ${font_content} 0 ${marginX+100} ${curHeight} ${item.createTime}\n`
	curHeight += font_content + content_margin_bottom

	pritCode += `FORM\n`
	pritCode += `PRINT\n`
	return pritCode
}

// 芝柯简化版
function simple_zhike(item, qrcodeText, isFrist) {
	let marginX = 30,
		marginY = 0,
		pageWidth = 600,
		pageHeight = 400,
		font_title = 55,
		font_content = 24,
		title_margin_bottom = 20,
		content_margin_bottom = 50,
		content_padding_left = 200,
		curHeight = marginY
	let pritCode = ''

	if (isFrist && Vue.prototype.$store.getters.printCalibration) {
		pritCode += `! 0 200 200 ${pageHeight} 1\n`
		pritCode += `PAGE-WIDTH ${pageWidth}\n`
		pritCode += `GAP-SENSE\n`
		pritCode += `CENTER\n`
		pritCode += `SETMAG 2 2\n`
		pritCode += `TEXT ${font_title} 0 0 ${curHeight} —— 接下来将为您打印合格证 ——\n`
		pritCode += `SETMAG 0 0\n`
		pritCode += `FORM\n`
		pritCode += `PRINT\n`
	}
	pritCode += `! 0 200 200 ${pageHeight} 1\n`
	pritCode += `PAGE-WIDTH ${pageWidth}\n`
	pritCode += `GAP-SENSE\n`
	pritCode += `SETMAG 2 2\n`
	pritCode += `CENTER\n`
	pritCode += `TEXT ${font_title} 32 0 ${curHeight} 食用农产品合格证\n`
	curHeight += font_title + title_margin_bottom

	pritCode += `SETMAG 0 0\n`
	pritCode += `LEFT\n`
	pritCode += `BARCODE QR ${marginX} ${curHeight} M 2 U 4\n`
	// pritCode += `MA,${item.longtime.replace(/"/g,'')+Vue.prototype.$prefixZero(item.startNum,4)}\n`
	pritCode += `MA,${qrcodeText}\n`
	pritCode += `ENDQR\n`

	pritCode += `TEXT ${font_content} 0 ${marginX+content_padding_left} ${curHeight} 产品名称:\n`
	pritCode += `TEXT ${font_content} 0 ${marginX+content_padding_left+120} ${curHeight} ${item.name}\n`
	curHeight += font_content + content_margin_bottom

	pritCode += `TEXT ${font_content} 0 ${marginX+content_padding_left} ${curHeight} 产品重量:\n`
	pritCode +=
		`TEXT ${font_content} 0 ${marginX+content_padding_left+120} ${curHeight} ${item.weight}${item.unit}\n`
	curHeight += font_content + content_margin_bottom

	pritCode += `TEXT ${font_content} 0 ${marginX+content_padding_left} ${curHeight} 开具日期:\n`
	pritCode += `TEXT ${font_content} 0 ${marginX+content_padding_left+120} ${curHeight} ${item.createTime}\n`
	curHeight += font_content + content_margin_bottom

	pritCode += `FORM\n`
	pritCode += `PRINT\n`
	return pritCode
}

// 佳博标准版
function default_jiabo(item, qrcodeText, isFrist) {
	let marginX = 30,
		marginY = 30,
		pageWidth = 600,
		pageHeight = 400,
		pageGap = 2,
		font_title = 24,
		font_content = 20,
		title_margin_bottom = 12,
		content_margin_bottom = 12,
		curHeight = marginY
	let pritCode = ''

	if (isFrist && Vue.prototype.$store.getters.printCalibration) {
		pritCode += `SIZE ${pageWidth/8} mm,${pageHeight/8} mm\r\n`
		pritCode += `GAP ${pageGap} mm\r\n`
		pritCode += `CLS\r\n`
		pritCode += `TEXT ${pageWidth/2-marginX-font_title*8},${curHeight},"TSS24.BF2",0,1,1,"—— 接下来将为您打印合格证 ——"\r\n`
		// pritCode += `HOME\r\n`
		pritCode += `PRINT 1\r\n`
	}

	pritCode += `SIZE ${pageWidth/8} mm,${pageHeight/8} mm\r\n`
	pritCode += `GAP ${pageGap} mm\r\n`
	pritCode += `CLS\r\n`
	pritCode += `TEXT ${pageWidth/2-marginX-font_title*4},${curHeight},"TSS24.BF2",0,1,1,"食用农产品合格证"\r\n`
	curHeight += font_title + title_margin_bottom

	pritCode += `TEXT ${marginX},${curHeight},"TSS20.BF2",0,1,1,"产品名称:"\r\n`
	pritCode += `TEXT ${marginX+100},${curHeight},"TSS20.BF2",0,1,1,"${item.name}"\r\n`
	curHeight += font_content + content_margin_bottom

	pritCode += `TEXT ${marginX},${curHeight},"TSS20.BF2",0,1,1,"产品重量:"\r\n`
	pritCode += `TEXT ${marginX+100},${curHeight},"TSS20.BF2",0,1,1,"${item.weight}${item.unit}"\r\n`
	curHeight += font_content + content_margin_bottom

	let wordLength, actWordLength, cnt

	pritCode += `TEXT ${marginX},${curHeight},"TSS20.BF2",0,1,1,"产品产地:"\r\n`
	wordLength = 20
	actWordLength = (item.area || '').length
	if (actWordLength > wordLength) {
		cnt = Math.ceil(actWordLength / wordLength)
		for (let i = 0; i < cnt; i++) {
			pritCode +=
				`TEXT ${marginX+100},${curHeight},"TSS20.BF2",0,1,1,"${item.area.substr(i*wordLength,wordLength)}"\r\n`
			curHeight += font_content + content_margin_bottom
		}
	} else {
		pritCode += `TEXT ${marginX+100},${curHeight},"TSS20.BF2",0,1,1,"${item.area}"\r\n`
		curHeight += font_content + content_margin_bottom
	}

	let wordLength2 = 18
	// pritCode +=
	// 	`QRCODE ${marginX+wordLength2*(font_content)+30},${curHeight},L,4,A,0,"${item.longtime.replace(/"/g,'')+Vue.prototype.$prefixZero(item.startNum,4)}"\n`
	pritCode += `QRCODE ${marginX+wordLength2*(font_content)+30},${curHeight},L,3,A,0,"${qrcodeText}"\n`

	pritCode += `TEXT ${marginX},${curHeight},"TSS20.BF2",0,1,1,"产品收购者:"\r\n`
	wordLength = 12
	actWordLength = (item.buyerName || '').length
	if (actWordLength > wordLength) {
		cnt = Math.ceil(actWordLength / wordLength)
		for (let i = 0; i < cnt; i++) {
			pritCode +=
				`TEXT ${marginX+120},${curHeight},"TSS20.BF2",0,1,1,"${item.buyerName.substr(i*wordLength,wordLength)}"\r\n`
			curHeight += font_content + content_margin_bottom
		}
	} else {
		pritCode += `TEXT ${marginX+120},${curHeight},"TSS20.BF2",0,1,1,"${item.buyerName}"\r\n`
		curHeight += font_content + content_margin_bottom
	}

	pritCode += `TEXT ${marginX},${curHeight},"TSS20.BF2",0,1,1,"地址:"\r\n`
	wordLength = 16
	actWordLength = (item.buyerAddress || '').length
	if (actWordLength > wordLength) {
		cnt = Math.ceil(actWordLength / wordLength)
		for (let i = 0; i < cnt; i++) {
			pritCode +=
				`TEXT ${marginX+60},${curHeight},"TSS20.BF2",0,1,1,"${item.buyerAddress.substr(i*wordLength,wordLength)}"\r\n`
			curHeight += font_content + content_margin_bottom
		}
	} else {
		pritCode += `TEXT ${marginX+60},${curHeight},"TSS20.BF2",0,1,1,"${item.buyerAddress}"\r\n`
		curHeight += font_content + content_margin_bottom
	}

	pritCode += `TEXT ${marginX},${curHeight},"TSS20.BF2",0,1,1,"开具日期:"\r\n`
	pritCode += `TEXT ${marginX+100},${curHeight},"TSS20.BF2",0,1,1,"${item.createTime}"\r\n`
	curHeight += font_content + content_margin_bottom

	pritCode += `PRINT 1\r\n`
	return pritCode
}

// 佳博简化版
function simple_jiabo(item, qrcodeText, isFrist) {
	let marginX = 30,
		marginY = 30,
		pageWidth = 600,
		pageHeight = 400,
		pageGap = 2,
		font_title = 32,
		font_content = 24,
		title_margin_bottom = 30,
		content_margin_bottom = 50,
		content_padding_left = 210,
		curHeight = marginY
	let pritCode = ''

	if (isFrist && Vue.prototype.$store.getters.printCalibration) {
		pritCode += `SIZE ${pageWidth/8} mm,${pageHeight/8} mm\r\n`
		pritCode += `GAP ${pageGap} mm\r\n`
		pritCode += `CLS\r\n`
		pritCode += `TEXT ${pageWidth/2-marginX-font_title*4},${curHeight},"TSS24.BF2",0,1,1,"—— 接下来将为您打印合格证 ——"\r\n`
		// pritCode += `HOME\r\n`
		pritCode += `PRINT 1\r\n`
	}

	pritCode += `SIZE ${pageWidth/8} mm,${pageHeight/8} mm\r\n`
	pritCode += `GAP ${pageGap} mm\r\n`
	pritCode += `CLS\r\n`
	pritCode += `TEXT ${pageWidth/2-marginX-font_title*4},${curHeight},"TSS32.BF2",0,1,1,"食用农产品合格证"\r\n`
	curHeight += font_title + title_margin_bottom

	// pritCode +=
	// 	`QRCODE ${marginX},${curHeight},L,8,A,0,"${item.longtime.replace(/"/g,'')+Vue.prototype.$prefixZero(item.startNum,4)}"\n`
	pritCode += `QRCODE ${marginX},${curHeight},L,5,A,0,"${qrcodeText}"\n`

	pritCode += `TEXT ${marginX+content_padding_left},${curHeight},"TSS24.BF2",0,1,1,"产品名称:"\r\n`
	pritCode += `TEXT ${marginX+content_padding_left+120},${curHeight},"TSS24.BF2",0,1,1,"${item.name}"\r\n`
	curHeight += font_content + content_margin_bottom

	pritCode += `TEXT ${marginX+content_padding_left},${curHeight},"TSS24.BF2",0,1,1,"产品重量:"\r\n`
	pritCode +=
		`TEXT ${marginX+content_padding_left+120},${curHeight},"TSS24.BF2",0,1,1,"${item.weight}${item.unit}"\r\n`
	curHeight += font_content + content_margin_bottom

	pritCode += `TEXT ${marginX+content_padding_left},${curHeight},"TSS24.BF2",0,1,1,"开具日期:"\r\n`
	pritCode += `TEXT ${marginX+content_padding_left+120},${curHeight},"TSS24.BF2",0,1,1,"${item.createTime}"\r\n`
	curHeight += font_content + content_margin_bottom

	pritCode += `PRINT 1\r\n`
	return pritCode
}

module.exports = {
	createPrintCode: createPrintCode
}
